package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.ShoppingCartMapper;
import cn.wolfcode.trip.base.query.CreditShopQuery;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("users")
public class UserController {
    @Autowired
    private ServletContext ctx;
    @Autowired
    private IUserService userService;
    @Autowired
    private ITravelService travelService;

    @PostMapping
    public JSONResult regist(User user){
        JSONResult result = new JSONResult();
        try {
            User u = userService.regist(user);
            //将注册用户对象信息塞入data属性中
            result.setData(u);
        }catch (Exception e){
            result.mark(e.getMessage());
        }
        return result;
    }

    @PutMapping
    public JSONResult update(User user){
        JSONResult result = new JSONResult();
        try {
            userService.update(user);
            result.setData(userService.get(user.getId()));
        }catch (Exception e){
            result.mark(e.getMessage());
        }
        return result;
    }
    @RequestMapping("/spendCredits")
    public JSONResult spendCredits(MyCredit myCredit){
        JSONResult result = new JSONResult();
        try {
            userService.spendCredits(myCredit);
        }catch (Exception e){
            result.mark(e.getMessage());
        }
        return result;
    }
    @RequestMapping("/mypricious")
    public void addPricious(MyPricious myPricious) {
        userService.addPricious(myPricious);
    }

    @RequestMapping("/addProducts")
    public void addProducts(ShoppingCart shoppingCart) {
        userService.addProducts(shoppingCart);
    }

    @RequestMapping("/deleteProducts")
    public void deleteProducts(ShoppingCart shoppingCart) {
        userService.deleteProducts(shoppingCart);
    }

    @RequestMapping("/addOrder")
    public void addOrder(Order order) {
        userService.addOrder(order);
    }

    @RequestMapping("/{userId}/ordersDetail")
    public List<Map<String, Object>> getOrdersDetail(@PathVariable Long userId) {
        return userService.selectOrder(userId);
    }

    @GetMapping("/{authorId}/travels")
    public Object list(TravelQuery qo){
        return travelService.selectForList(qo);
    }

    @GetMapping("/{userId}/credits")
    public Object getCredits(@PathVariable Long userId){
        return userService.selectCredits(userId);
    }


    @GetMapping
    public Object selectAll(){
        return userService.selectAll();
    }


    @GetMapping("/{userId}/shoppingCart")
    public void getShoppingCart(@PathVariable Long userId, HttpServletResponse rep) throws Exception {
        List<Map<String, Object>> shoppingCart = userService.selectShoppingCart(userId);
        //模板 + 数据 = 输出
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
        cfg.setDirectoryForTemplateLoading(new File(ctx.getRealPath("/template")));
        Template template = cfg.getTemplate("shoppingCart.ftl", "utf-8");

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("shoppingCart", shoppingCart);
        //输出到浏览器
        template.process(data, rep.getWriter());
    }
    @GetMapping("/{userId}/orders")
    public void getOrder(@PathVariable Long userId, HttpServletResponse rep) throws Exception {
        List<Map<String, Object>> orders = userService.selectOrder(userId);
        //模板 + 数据 = 输出
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
        cfg.setDirectoryForTemplateLoading(new File(ctx.getRealPath("/template")));
        Template template = cfg.getTemplate("order.ftl", "utf-8");

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("orders", orders);
        //输出到浏览器
        template.process(data, rep.getWriter());
    }

}
